<?php
    require_once('paths.inc');
    require_once(INCLUDE_PATH.'constants.inc');
    require_once('page.inc');
    require_once('server_validations.inc');
    require_once(DB_PATH.'db.inc');
    
    require_once('security.inc');
    require_once('utils.inc');    
    session_start();
    $id_client = $_SESSION['user_logat']->id_client;    
	echo '<br />';
	echo '<h3>PAGINA: Validare plati</h3>';

    //we do the post thing
    $db = db_connect();
    try {
        $facturi = FacturaClient::getFacturiNeachitate($db);
    }
    catch (QueryGetFacturiNeachitateException $e) {
        var_dump($e->getMessage());
    } 
        
    for ($i = 0; $i < count($facturi); $i++) {
        if (isset($_POST['v' . $i])) {
            $factura_id = $_POST['f' . $i];
            $data_sfarsit = $_POST['d_sf' . $i];
            $nume = $_POST['nume' . $i];
            try {
                FacturaClient::updateFacturaAsAchitata($db, $factura_id, $id_client, $data_sfarsit);
                echo "<div align=\"center\">Factura pentru clientul <b>$nume</b> a fost validata cu succes!</div>";
            }
            catch (QueryUpdateFacturaAsAchitataException $e) {
                var_dump($e->getMessage());
            }
        }
    }
    try {
        $facturi = FacturaClient::getFacturiNeachitate($db);
    }
    catch (QueryGetFacturiNeachitateException $e) {
        var_dump($e->getMessage());
    }    
?>
    <form name="form1" action="" method="post">
        <table align="center" border="0" cellspacing="2" cellpadding="2">
            <tr>
                <td bgcolor="#CCCCCC" align="center"><b>Factura ID</b></td>
                <td bgcolor="#CCCCCC" align="center"><b>Data facturii</b></td>
                <td bgcolor="#CCCCCC" align="center"><b>Suma</b></td>
                <td bgcolor="#CCCCCC" align="center"><b>Data inceput</b></td>
                <td bgcolor="#CCCCCC" align="center"><b>Data sfarsit</b></td>
                <td bgcolor="#CCCCCC" align="center"><b>Numar factura</b></td>
                <td bgcolor="#CCCCCC" align="center"><b>Nume client</b></td>
                <td bgcolor="#CCCCCC" align="center"><b>Valideaza plata</b></td>
            </tr>
            <?php
                if (count($facturi) > 0) {
                    for ($i = 0; $i < count($facturi); $i++) {
                        echo "<tr>";
                            echo "<td>" . $facturi[$i]['id'] . "</td>";
                            echo "<td>" . $facturi[$i]['data'] . "</td>";
                            echo "<td>" . $facturi[$i]['suma'] . "</td>";
                            echo "<td>" . $facturi[$i]['data_inceput'] . "</td>";
                            echo "<td>" . $facturi[$i]['data_sfarsit'] . "</td>";
                            echo "<td>" . $facturi[$i]['nr_factura'] . "</td>";
                            echo "<td>" . $facturi[$i]['nume'] . "</td>";
                            echo "<td><input type=\"submit\" value=\"Valideaza\" name=\"v{$i}\" /></td>";
                            echo "<input type=\"hidden\" value=\"{$facturi[$i]['id']}\" name=\"f{$i}\" /></td>";
                            echo "<input type=\"hidden\" value=\"{$facturi[$i]['data_sfarsit']}\" name=\"d_sf{$i}\" /></td>";
                            echo "<input type=\"hidden\" value=\"{$facturi[$i]['nume']}\" name=\"nume{$i}\" /></td>";
                        echo "</tr>";
                    }
                }
                else echo '<tr bgcolor="#CCCCCC"><td align="center" colspan="8">Nu exista facturi inca</td></tr>';
            ?>
        </table>
    </form>